﻿using UnityEngine;
using System.Collections;
using System.Collections.Generic;

public class chatSystem : MonoBehaviour {
	
	public string chatMessage = "";
	public List<string> allMesseges;
	private Vector2 scrollPosition = Vector2.zero;
	
	private int maxMessage = 64;
	public GUISkin mySkin;
	//public AudioClip buzzSound;
	public AudioClip messageSound;
	private int maxScrollSize = 1000;
	public Rect chatBoxRect;
	
	private DataScript data;
	
	private Rect scrollViewRect;
	private Rect scrollViewSourceR;
	private Rect textFieldRect;
	private int typeBarHeight = 20;
	
	void Awake()
	{
		//if(allMesseges != null)
		//	allMesseges.Clear();
		//chatMessage = "";
	}
	
	void Start()
	{
		int xPos = (int)(Screen.width * chatBoxRect.x / 100);
		//Debug.Log(Screen.width);
		int yPos = (int)(Screen.height * chatBoxRect.y / 100);
		//making the scrollview reference point at the middle for easy scaling
		scrollViewRect = new Rect(873, 387,
								  348, 196);
		scrollViewSourceR = new Rect(0,0,chatBoxRect.width - typeBarHeight,maxScrollSize);
		textFieldRect = new Rect(1010, 636, 260, typeBarHeight);
		
		data = GameObject.FindGameObjectWithTag("DataManager").GetComponent<DataScript>();
		scrollPosition.y = maxScrollSize;
		allMesseges = new List<string>();
	}
	
	public void sendMessage(string message)
	{
		//Debug.Log("AW");
		message = data.playerName + ": " + message;
		networkView.RPC("RPC_sendMessage", RPCMode.Others, message);
		allMesseges.Add(message);
		if(allMesseges.Count > maxMessage)
			allMesseges.RemoveAt(0);
	}
	
	public virtual void receivedMessage(string message)
	{
		allMesseges.Add(message);
		audio.PlayOneShot(messageSound);
		if(allMesseges.Count > maxMessage)
			allMesseges.RemoveAt(0);
	}
	
	public void scrollBarHandler()
	{
		scrollPosition.y = maxScrollSize * (1 - UISlider.current.value);	
	}
	
	void OnGUI()
	{
		//if Enter KEY_DOWN
		if (Event.current.type == EventType.KeyDown && Event.current.keyCode == KeyCode.Return)
		{
			if(GUI.GetNameOfFocusedControl() == "ChatBox")
			{
				if(chatMessage != "")
				{
					sendMessage(chatMessage);
					chatMessage = "";
				}
				else
					GUI.FocusControl("");
			}
			else
				GUI.FocusControl("ChatBox");
		}
		
		var scrollbarStyle = new GUIStyle(GUI.skin.horizontalScrollbar);

		scrollbarStyle.fixedHeight = scrollbarStyle.fixedWidth = 0;
		//Debug.Log(scrollPosition);
		//scrollPosition = 
		GUI.BeginScrollView(scrollViewRect, scrollPosition, scrollViewSourceR,GUIStyle.none,GUIStyle.none);
		int cBLabeloffSet = 20;
		//decrementing loop so it can print from bottom newest to top oldest
		for(int i = allMesseges.Count - 1; i >= 0; i--)
		{
			GUI.Label(new Rect(0, maxScrollSize - cBLabeloffSet, 330, 20),allMesseges[i],mySkin.customStyles[0]);

			//Vector2 size = mySkin.customStyles[0].CalcSize(new GUIContent(allMesseges[i]));
			int height = (int)mySkin.customStyles[0].CalcHeight(new GUIContent(allMesseges[i]),330);
			cBLabeloffSet += height;
		}
		
		GUI.EndScrollView();
		
		GUI.SetNextControlName("ChatBox");
		chatMessage = GUI.TextField(textFieldRect, chatMessage,mySkin.customStyles[1]);
		
		//if(GUI.Button(new Rect(500,400,100,50), "BUZZ"))
		//{
		//	networkView.RPC("RPC_Notify", RPCMode.All);
		//}
	}
	
	[RPC]
	void RPC_sendMessage(string message)
	{
		receivedMessage(message);
	}
	
	[RPC]
	void RPC_Notify()
	{
		//audio.PlayOneShot(buzzSound);
	}
}
